#ifndef _SHADOW_UTIL_INI_PARSER_H_
#define _SHADOW_UTIL_INI_PARSER_H_

#include <string>
#include <map>
#include <list>

#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>

#define SUTIL_INI_DEFAULT_SESSION_NAME "_SUTIL_DEFAULT_SESSION_"
#define SUTIL_INI_COMMENT_CHAR '#'
#define SUTIL_INI_KEY_VALUE_SEP_CHAR '='
#define SUTIL_INI_SESSION_START_CHAR '['
#define SUTIL_INI_SESSION_END_CHAR ']'

using std::string;
using std::map;
using std::list;

typedef map<string, string> StringMap;

class INIParser {

		public:
				INIParser();
				virtual ~INIParser();

				int parse(const char * ini_file);

				int get_int_value(const char * session, const char * key, int & value);
				int get_string_value(const char * session, const char * key, string & value);
				int get_bool_value(const char * session, const char * key, bool & value);
				int get_long_long_value(const char * session, const char * key, long long & value);

				map<string, StringMap> & get_contents();

		protected:
				int read_whole_file_to_buf(const char * file_path, string & buf);
				int split_buf_to_lines(const char *buf, const int len, list<string> & lines);
				bool is_session_line(const char * buf, const int len, string & session_name);
				int trim(const char * buf, const int len, string & out);
				int split_line_key_and_value(const char * cbuf, const int len, string & key, string & value);
				int to_lower(const char * str, const int len, string & out);
				int get_raw_value(const char * session, const char * key, string & value);
		private:
				log4cxx::LoggerPtr _logger;
				string _ini_file_path;
				map<string, StringMap> _contents;

};

#endif
